﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Prism.Interfaces;
using System.Windows;
using Prism.Commands;
using EmployeeModule.EmployeeChooser;
using System.Diagnostics;
using Domain;
using EmployeeModule.EmployeeDetail;
using System.Windows.Input;
using System.Windows.Controls;

namespace EmployeeModule.EmployeeMain
{
    public static class EmployeeMainCommands
    {
        public static RoutedCommand AddEmployee = new RoutedCommand();

    }

    public class EmployeeMainController: IEmployeeMainController
    {
        private IUnityContainer container;
        private IEmployeeMainView view;

        private IEmployeeDetailController details;


        public EmployeeMainController(IUnityContainer container, IEmployeeMainView view)
        {
            this.container = container;
            this.view = view;

            ((UIElement) this.view).CommandBindings.Add(
                new CommandBinding(EmployeeChooserCommands.ChooseEmployee,
                    Choose));

            ((UIElement) this.view).CommandBindings.Add(
    new CommandBinding(EmployeeMainCommands.AddEmployee,
        AddNewEmployee));

            details =  container.Resolve<IEmployeeDetailController>();

        }


        public void Show()
        {
            // we want to show ourself
            IRegionManagerService regionManager = container.Resolve<IRegionManagerService>();
            regionManager.GetRegion("employeeRegion").Add( (UIElement) view);


        }

        public void Choose(object sender, RoutedEventArgs e)
        {
            IEmployeeChooserController chooser = container.Resolve<IEmployeeChooserController>();
            chooser.Show(delegate(Employee chosen)
            {
                if (chosen != null)
                {
                    IRegionManagerService regionManager = container.Resolve<IRegionManagerService>();
                    IRegion detailRegion = regionManager.GetRegion("employeeDetail");

                    details.Show(detailRegion, chosen);
                }
            });
        }


        void AddNewEmployee(object sender, RoutedEventArgs e)
        {
            string eType = ((ComboBoxItem) ((ExecutedRoutedEventArgs) e).Parameter).Content as string;

            Employee newE = null;

            // call your factory here
            if (typeof(Mort).Name.EndsWith(eType))
            {
                newE = new Mort();
            }
            else if (typeof(Elvis).Name.EndsWith(eType))
            {
                newE = new Elvis();
            }
            else if (typeof(Einstein).Name.EndsWith(eType))
            {
                newE = new Einstein();
            }

            newE.TeamMembers = new List<Employee>();
            newE.Firstname = "firstname";
            newE.Lastname = "lastname";

            IRegionManagerService regionManager = container.Resolve<IRegionManagerService>();
            IRegion detailRegion = regionManager.GetRegion("employeeDetail");

            details.Show(detailRegion, newE);

        }
    }
}
